package com.manager.kudian.system.service;

import com.manager.kudian.common.persistence.HibernateDao;
import com.manager.kudian.common.persistence.Page;
import com.manager.kudian.common.persistence.PropertyFilter;
import com.manager.kudian.common.service.BaseService;
import com.manager.kudian.common.utils.PropertiesLoader;
import com.manager.kudian.common.utils.StringUtils;
import com.manager.kudian.system.dao.CategoryDao;
import com.manager.kudian.system.dao.GroupBuyDao;
import com.manager.kudian.system.dao.SellerDao;
import com.manager.kudian.system.dao.UsersDao;
import com.manager.kudian.system.entity.Category;
import com.manager.kudian.system.entity.GroupBuy;
import com.manager.kudian.system.entity.ManagerUser;
import com.manager.kudian.system.entity.Seller;
import org.apache.log4j.Logger;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/4/14.
 */
@Service
@Transactional(readOnly = true)
public class SellerService extends BaseService<Seller, Integer> {

    private static final Logger logger = Logger.getLogger(SellerService.class);

    @Autowired
    private SellerDao sellerDao;

    @Autowired
    private UsersDao usersDao;

    @Autowired
    private CategoryDao categoryDao;

    @Autowired
    private GroupBuyDao groupBuyDao;

    @Override
    public HibernateDao<Seller, Integer> getEntityDao() {
        return sellerDao;
    }

    @Transactional(readOnly = true)
    public Page<Seller> query(final Page<Seller> page) {
        Disjunction dis = Restrictions.disjunction();
        return getEntityDao().findPage(page, Restrictions.in("sellerStatus", new Short[]{0, 1}));
    }

    /**
     * 审核通过
     * @param id
     * @return
     */
    @Transactional(readOnly = false)
    public void auditSeller(Integer id, ManagerUser u) {

        Seller s = sellerDao.find(id);
        // 0：新建  1: 审核中 2：通过审核  3：未通过审核 4：取消资格
        s.setSellerStatus((short) 2);
        s.setModifyTime(new Timestamp(System.currentTimeMillis()));
        s.setAuditTime(new Timestamp(System.currentTimeMillis()));
        save(s);

        // 根据商家的类型添加产品类别
        // 在category.properties文件中以CATEGORY_ + gtid 为主键
        String key = "CATEGORY_" + s.getGtId();
        PropertiesLoader p = new PropertiesLoader("category.properties");
        String categories = p.getProperty(key);
        if (StringUtils.isNotBlank(categories)) {
            String[] cats = categories.split(",");
            short ordersort = 1;
            for (String cat : cats) {
                Category c = new Category();
                c.setSellerId(s.getSellerId());
                c.setCatName(cat);
                c.setKeywords("");
                c.setParentId(0);
                c.setCatDesc("");
                c.setSortOrder(ordersort);
                ordersort++;
                categoryDao.save(c);
            }
        }

        GroupBuy bg = new GroupBuy();
        bg.setName("午团");
        bg.setType((short) 1);
        bg.setDescription("午团");
        bg.setAddTime(new Timestamp(System.currentTimeMillis()));
        bg.setBookingTm("10:00");
        bg.setDeliveryTm("12:00");
        bg.setSellerId(id);

        GroupBuy bg1 = new GroupBuy();
        bg1.setName("晚团");
        bg1.setType((short) 1);
        bg1.setDescription("晚团");
        bg1.setAddTime(new Timestamp(System.currentTimeMillis()));
        bg1.setBookingTm("16:00");
        bg1.setDeliveryTm("18:00");
        bg1.setSellerId(id);
        this.groupBuyDao.save(bg);
        this.groupBuyDao.save(bg1);
    }

    /**
     * 审核不通过
     * @param id
     * @param u
     */
    @Transactional(readOnly = false)
    public void auditNopassSeller(Integer id, ManagerUser u) {
        Seller s = sellerDao.find(id);
        // 0：新建  1: 审核中 2：通过审核  3：未通过审核 4：取消资格
        s.setSellerStatus((short) 3);
        s.setModifyTime(new Timestamp(System.currentTimeMillis()));
        save(s);
    }

    /**
     * 更新店铺信息
     * @param id
     * @param name               店铺名称
     * @param corporation       法人
     * @param bankAccount       提款时的银行账号
     */
    @Transactional(readOnly = false)
    public void updateinfo(Integer id, String name, String corporation, String bankAccount) {
        Seller s = sellerDao.find(id);

        s.setName(name);
        s.setCorporation(corporation);
        s.setBankAccount(bankAccount);
        s.setModifyTime(new Timestamp(System.currentTimeMillis()));
        sellerDao.save(s);
    }

    public boolean checkSellerName(Integer id) {
        Seller s = sellerDao.get(id);
        if (s == null || StringUtils.isBlank(s.getName())) {
            return false;
        }
        return true;
    }
}
